package com.hkctp.consumer;

import org.apache.kafka.clients.consumer.ConsumerRecord;
import org.apache.kafka.clients.consumer.ConsumerRecords;
import org.apache.kafka.clients.consumer.KafkaConsumer;
import org.apache.kafka.clients.producer.ProducerConfig;

import java.util.Arrays;
import java.util.Collections;
import java.util.Properties;

public class CustomerConsumer {

    public static void main(String[] args){
        //配置信息
        Properties props = new Properties();
        //kafka集群
        props.put("bootstrap.servers","localhost:9092");
        //消费者组id
        props.put("group.id","test");
        //设置自动提交offset
        props.put("enable.auto.commit","true");
        //提交延时
        props.put("auto.commit.interval.ms","1000");
        //提供序列化的类   kv的反序列化类
        props.put("key.deserializer","org.apache.kafka.common.serialization.StringDeserializer");
        props.put("value.deserializer","org.apache.kafka.common.serialization.StringDeserializer");

        //创建消费者对象
        KafkaConsumer<String,String> consumer = new KafkaConsumer<String,String>(props);
        //指定topic
        consumer.subscribe(Arrays.asList("first","second","thrid"));

        while (true){
            ConsumerRecords<String,String> consumerRecords = consumer.poll(100);

            for (ConsumerRecord<String, String> records : consumerRecords){
                System.out.println(records.topic()+"---"+records.partition() + "---" + records.value());
            }
        }

    }
}
